package com.tanhua.dubbo.api;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


/**
 * @author ck
 * @date 2021/11/15 13:14
 */

@DubboService
public class CommentApiImpl implements CommentApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    //发布评论并且获取评论数量
    public Integer save(Comment comment1) {
        //1、查询动态
        Movement movement = mongoTemplate.findById(comment1.getPublishId(), Movement.class);
        //2、设置被评论人属性
        if (movement != null) {
            comment1.setPublishUserId(movement.getUserId());
        }
        //3、保存到数据库
        mongoTemplate.save(comment1);
        //4、更新动态表中的字段
        Query query = Query.query(Criteria.where("id").is(comment1.getPublishId()));
        Update update = new Update();
        if (comment1.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comment1.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true); //获取更新后的最新数据
        Movement movement1 = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //5、获取最新评论数并返回
        return movement1.statisCount(comment1.getCommentType());
    }

    @Override
    //分页查询评论
    public List<Comment> findCommentList(String movementId, CommentType comment, Integer page, Integer pagesize) {
        //1、构造查询条件
        Query query = Query.query(Criteria.where("publishId")
                .is(new ObjectId(movementId))
                .and("commentType")
                .is(comment.getType()))
                .skip((page -1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //2、查询并返回
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    //判断comment数据是否存在
    @Override
    public boolean hasLiked(String movementId, Long userId, CommentType like) {
        //判断comment数据是否存在
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(like.getType()));
        //判断数据是否存在
        boolean exists = mongoTemplate.exists(query, Comment.class);
        return exists;
    }

    //删除
    @Override
    public Integer delete(Comment comment) {
        //1、删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());

        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        //2、修改动态表中的总数量
        Query movementQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if(comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //3、设置更新参数
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true); //获取更新后的最新数据
        Movement movement = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
        //5、获取最新的评论数量，并返回
        return movement.statisCount(comment.getCommentType());
    }

    @Override
    public boolean hasLoved(String movementId, Long userId, CommentType love) {
        //判断comment数据是否存在
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(love.getType()));
        //判断数据是否存在
        return mongoTemplate.exists(query,Comment.class); //判断数据是否存在
    }


    @Override
    public Integer likevAndUnlike(String commentId, String type) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(commentId)));
        boolean exists = mongoTemplate.exists(query, Comment.class);
        if (!exists)
            return -1;
        Update update = new Update();
        if (type.equals("zan")){
            update.inc("likeCount",1);
        }else {
            update.inc("likeCount",-1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true); //获取更新后的最新数据
        Comment comment = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return comment.getLikeCount();
    }

    @Override
    public List<Comment> findList(Long userId) {
        Query query = Query.query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(CommentType.LIKE.getType()));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return commentList;
    }

    @Override
    public List<Comment> findLoveList(Long userId) {
        Query query = Query.query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(CommentType.LOVE.getType()));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return commentList;
    }

    @Override
    public List<Comment> findCommentsList(Long userId) {
        Query query = Query.query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(CommentType.COMMENT.getType()));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return commentList;
    }

    @Override
    public PageResult findComment(Integer page, Integer pagesize, ObjectId publishId) {
        Query query = Query.query(Criteria.where("publishId").is(publishId).
                and("commentType").is(CommentType.COMMENT.getType())).
                skip((page - 1) * pagesize).
                limit(pagesize);
        long count = mongoTemplate.count(query, Comment.class);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        return new PageResult(page,pagesize,count,commentList);
    }

}
